﻿// Decompiled with JetBrains decompiler
// Type: 我的小工具.MultiColumnComboBox
// Assembly: 开发辅助工具, Version=1.0.0.1, Culture=neutral, PublicKeyToken=null
// MVID: F78E5980-5E94-4E46-ABDF-DC8C6CBF52AA
// Assembly location: K:\Tools\工具\开发辅助工具庆六一版\开发辅助工具2015-06-01\开发辅助工具.exe

using System;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;

namespace 我的小工具
{
  public class MultiColumnComboBox : ComboBox
  {
    private float[] columnWidths = new float[0];
    private string[] columnNames = new string[0];
    private int valueMemberColumnIndex = 0;
    private const int columnPadding = 5;

    public new DrawMode DrawMode
    {
      get
      {
        return base.DrawMode;
      }
      set
      {
        if (value != DrawMode.OwnerDrawVariable)
          throw new NotSupportedException("Needs to be DrawMode.OwnerDrawVariable");
        base.DrawMode = value;
      }
    }

    public new ComboBoxStyle DropDownStyle
    {
      get
      {
        return base.DropDownStyle;
      }
      set
      {
        if (value == ComboBoxStyle.Simple)
          throw new NotSupportedException("ComboBoxStyle.Simple not supported");
        base.DropDownStyle = value;
      }
    }

    public MultiColumnComboBox()
    {
      this.DrawMode = DrawMode.OwnerDrawVariable;
    }

    protected override void OnDataSourceChanged(EventArgs e)
    {
      base.OnDataSourceChanged(e);
      this.InitializeColumns();
    }

    protected override void OnValueMemberChanged(EventArgs e)
    {
      base.OnValueMemberChanged(e);
      this.InitializeValueMemberColumn();
    }

    protected override void OnDropDown(EventArgs e)
    {
      base.OnDropDown(e);
      this.DropDownWidth = (int) this.CalculateTotalWidth();
    }

    private void InitializeColumns()
    {
      PropertyDescriptorCollection itemProperties = this.DataManager.GetItemProperties();
      this.columnWidths = new float[itemProperties.Count];
      this.columnNames = new string[itemProperties.Count];
      for (int index = 0; index < itemProperties.Count; ++index)
      {
        string name = itemProperties[index].Name;
        this.columnNames[index] = name;
      }
    }

    private void InitializeValueMemberColumn()
    {
      int num = 0;
      foreach (string columnName in this.columnNames)
      {
        if (string.Compare(columnName, this.ValueMember, true, CultureInfo.CurrentUICulture) == 0)
        {
          this.valueMemberColumnIndex = num;
          break;
        }
        ++num;
      }
    }

    private float CalculateTotalWidth()
    {
      float num = 0.0f;
      foreach (int columnWidth in this.columnWidths)
        num += (float) (columnWidth + 5);
      return num + (float) SystemInformation.VerticalScrollBarWidth;
    }

    protected override void OnMeasureItem(MeasureItemEventArgs e)
    {
      base.OnMeasureItem(e);
      if (this.DesignMode)
        return;
      for (int index = 0; index < this.columnNames.Length; ++index)
      {
        string @string = Convert.ToString(this.FilterItemOnProperty(this.Items[e.Index], this.columnNames[index]));
        SizeF sizeF = e.Graphics.MeasureString(@string, this.Font);
        this.columnWidths[index] = Math.Max(this.columnWidths[index], sizeF.Width);
      }
      float totalWidth = this.CalculateTotalWidth();
      e.ItemWidth = (int) totalWidth;
    }

    protected override void OnDrawItem(DrawItemEventArgs e)
    {
      base.OnDrawItem(e);
      if (this.DesignMode)
        return;
      e.DrawBackground();
      Rectangle bounds = e.Bounds;
      int num = 0;
      using (Pen pen = new Pen(SystemColors.GrayText))
      {
        using (SolidBrush solidBrush = new SolidBrush(this.ForeColor))
        {
          if (this.columnNames.Length == 0)
          {
            e.Graphics.DrawString(Convert.ToString(this.Items[e.Index]), this.Font, (Brush) solidBrush, (RectangleF) bounds);
          }
          else
          {
            for (int index = 0; index < this.columnNames.Length; ++index)
            {
              string @string = Convert.ToString(this.FilterItemOnProperty(this.Items[e.Index], this.columnNames[index]));
              bounds.X = num;
              bounds.Width = (int) this.columnWidths[index] + 5;
              num = bounds.Right;
              if (index == this.valueMemberColumnIndex)
              {
                using (Font font = new Font(this.Font, FontStyle.Bold))
                  e.Graphics.DrawString(@string, font, (Brush) solidBrush, (RectangleF) bounds);
              }
              else
                e.Graphics.DrawString(@string, this.Font, (Brush) solidBrush, (RectangleF) bounds);
              if (index < this.columnNames.Length - 1)
                e.Graphics.DrawLine(pen, bounds.Right, bounds.Top, bounds.Right, bounds.Bottom);
            }
          }
        }
      }
      e.DrawFocusRectangle();
    }
  }
}
